


package CapaDueño;
import CapaSecretaria.cliente;
import Capamixta.MainMenu;
import capanegocio.bus;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/**
 *
 * @author Coco
 */
public class Bus extends javax.swing.JFrame {
    private final bus b;

    /**
     * Creates new form RegistroBuses
     */
    public Bus() {
        initComponents();
        b=new bus();
        setResizable(false);
        setLocationRelativeTo(null);
        setTitle("Registro de Buses");
        updateTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Titulo = new javax.swing.JLabel();
        PatBus = new javax.swing.JLabel();
        RutChofer = new javax.swing.JLabel();
        ModeloBus = new javax.swing.JLabel();
        AñoBus = new javax.swing.JLabel();
        CapacBus = new javax.swing.JLabel();
        Patente = new javax.swing.JTextField();
        Chofer_Rut_Chofer = new javax.swing.JTextField();
        Modelo = new javax.swing.JTextField();
        Año = new javax.swing.JTextField();
        Capacidad = new javax.swing.JTextField();
        Crear = new javax.swing.JButton();
        Eliminar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        TituloBase = new javax.swing.JLabel();
        tablascroll = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        Administrativo = new javax.swing.JMenuItem();
        Chofer = new javax.swing.JMenuItem();
        Salir = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Titulo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        Titulo.setText("Portal de Ingreso Nuevos Buses a la Empresa");

        PatBus.setText("Patente Bus");

        RutChofer.setText("Rut Chofer");

        ModeloBus.setText("Modelo");

        AñoBus.setText("Año");

        CapacBus.setText("Capacidad");

        Patente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PatenteActionPerformed(evt);
            }
        });

        Chofer_Rut_Chofer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Chofer_Rut_ChoferActionPerformed(evt);
            }
        });

        Crear.setText("Crear");
        Crear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CrearActionPerformed(evt);
            }
        });

        Eliminar.setText("Eliminar");
        Eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Datos de Bus");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Datos Chofer");

        jLabel3.setText("Tabla Buses");

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        tablascroll.setViewportView(tabla);

        jMenu1.setText("Menus");

        Administrativo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/User.png"))); // NOI18N
        Administrativo.setText("Creacion Administrativos");
        Administrativo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdministrativoActionPerformed(evt);
            }
        });
        jMenu1.add(Administrativo);

        Chofer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/User_Group.png"))); // NOI18N
        Chofer.setText("Registro Chofer");
        Chofer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChoferActionPerformed(evt);
            }
        });
        jMenu1.add(Chofer);

        Salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Logout.png"))); // NOI18N
        Salir.setText("Salir");
        Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirActionPerformed(evt);
            }
        });
        jMenu1.add(Salir);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(RutChofer)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(PatBus)
                                            .addGap(78, 78, 78))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jLabel1)
                                            .addGap(55, 55, 55)))
                                    .addGap(225, 225, 225)
                                    .addComponent(Año, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(ModeloBus, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(79, 79, 79)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(Modelo)
                                                .addComponent(Patente, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGap(40, 40, 40))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(Crear, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(53, 53, 53)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(CapacBus)
                                            .addGap(47, 47, 47)
                                            .addComponent(Capacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(AñoBus)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(10, 10, 10)
                                            .addComponent(Eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(TituloBase, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(154, 154, 154)
                            .addComponent(Chofer_Rut_Chofer, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(120, 120, 120)
                            .addComponent(Titulo))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(242, 242, 242)
                            .addComponent(jLabel3))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(tablascroll, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(Titulo)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RutChofer)
                    .addComponent(Chofer_Rut_Chofer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PatBus)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(AñoBus)
                        .addComponent(Año, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Patente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ModeloBus)
                        .addComponent(Modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(CapacBus)
                        .addComponent(Capacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Crear)
                    .addComponent(Eliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(tablascroll, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(TituloBase, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PatenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PatenteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PatenteActionPerformed

    private void SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirActionPerformed
        MainMenu obj=new MainMenu(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_SalirActionPerformed

    private void CrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CrearActionPerformed
        b.newbus(Patente.getText(), Chofer_Rut_Chofer.getText(), Modelo.getText() ,Integer.parseInt(Año.getText()) , Integer.parseInt(Capacidad.getText()) );
        TituloBase.setText("Bus Ingresado Satisfactoriamente");
        updateTabla();
        // TODO add your handling code here:
    }//GEN-LAST:event_CrearActionPerformed

    private void Chofer_Rut_ChoferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Chofer_Rut_ChoferActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Chofer_Rut_ChoferActionPerformed

    private void EliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarActionPerformed
        b.deletebus(Patente.getText());
        TituloBase.setText("Bus Eliminado de la Lista");
        limpiar();
        updateTabla();  
    }//GEN-LAST:event_EliminarActionPerformed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
            fila = tabla.rowAtPoint(evt.getPoint());                 
            if (fila > -1){  
             Patente.setText(String.valueOf(tabla.getValueAt(fila, 0)));
             Chofer_Rut_Chofer.setText(String.valueOf(tabla.getValueAt(fila, 1)));
             Modelo.setText(String.valueOf(tabla.getValueAt(fila, 2)));
             Año.setText(String.valueOf(tabla.getValueAt(fila, 3)));
             Capacidad.setText(String.valueOf(tabla.getValueAt(fila, 4)));
             
             }
                    // TODO add your handling code here:
    }//GEN-LAST:event_tablaMouseClicked

    private void AdministrativoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdministrativoActionPerformed
        user obj=new user(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_AdministrativoActionPerformed

    private void ChoferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChoferActionPerformed
        Chofer obj=new Chofer(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();// TODO add your handling code here:
    }//GEN-LAST:event_ChoferActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Bus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Bus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Bus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Bus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Bus().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Administrativo;
    private javax.swing.JTextField Año;
    private javax.swing.JLabel AñoBus;
    private javax.swing.JLabel CapacBus;
    private javax.swing.JTextField Capacidad;
    private javax.swing.JMenuItem Chofer;
    private javax.swing.JTextField Chofer_Rut_Chofer;
    private javax.swing.JButton Crear;
    private javax.swing.JButton Eliminar;
    private javax.swing.JTextField Modelo;
    private javax.swing.JLabel ModeloBus;
    private javax.swing.JLabel PatBus;
    private javax.swing.JTextField Patente;
    private javax.swing.JLabel RutChofer;
    private javax.swing.JMenuItem Salir;
    private javax.swing.JLabel Titulo;
    private javax.swing.JLabel TituloBase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTable tabla;
    private javax.swing.JScrollPane tablascroll;
    // End of variables declaration//GEN-END:variables

    Object [][] dtPer;
        int fila=-1;
        bus p=new bus();
        
        private void updateTabla(){
        String [] ColumnNames={"Patente", "Rut Chofer","Modelo","Año", "Capacidad"};
        dtPer=p.getDatos();
        DefaultTableModel Datos=new DefaultTableModel(dtPer,ColumnNames);
        tabla.setModel(Datos);
        TableColumn columna=tabla.getColumn("Rut Chofer");
        }

    private void limpiar() {
        Patente.setText("");
        Chofer_Rut_Chofer.setText("");
        Modelo.setText("");
        Año.setText("");
        Capacidad.setText("");
    }
}
